package shortcircuit.utilities;

import java.io.*;


/**
 * Allows the user to create map files via the command prompt.  This class is used mainly
 * for debugging purposes.
 * 
 * @author Kevin Lewi
 */
public class MapEditor
{
	private BufferedReader inputReader;
	private FileIO mapReader;
	
	/**
	 * Creates a new map editor from the main constructor
	 * 
	 * @param args Arguments passed into the class (unused)
	 * @author Kevin Lewi
	 */
	public static void main(String[] args)
	{
		new MapEditor();
	}
	
	/**
	 * Handles all of the system reading and passes this information to FileIO to create
	 * the file.<p>
	 * The format for the commands of working with files is as follows:<br>
	 * [Type of command followed by file path]<br>
	 * [Dimension of the Environment separated by a space]<br>
	 * [# of moves] [number of blocks to be removed]<br>
	 * [First row of cells]<br>
	 * [Second row of cells]<br>
	 * etc...<p>
	 * Example:<br>
	 * new testMap01.scmap<br>
	 * 4 2<br>
	 * 2 3<br>
	 * 1 4 3 5<br>
	 * 2 0 1 1<br>
	 * 
	 * @author Kevin Lewi
	 */
	public MapEditor()
	{
		Debug.on();
		
		inputReader = new BufferedReader(new InputStreamReader(System.in));
		try
		{
			String accessType = inputReader.readLine();
			if(accessType.contains("new"))
				createNewFile(accessType.substring(accessType.indexOf(" ") + 1));
			Debug.println("File was successfully created.");
		}
		catch(FileNotFoundException e) { Debug.println("File Not Found"); }
		catch(IOException e) { Debug.println("Error in IO"); }
	}
	
	/**
	 * Creates a new map file by calling the appropriate write methods in FileIO.
	 * 
	 * @param theFile The path of the file to be created
	 * @author Kevin Lewi
	 */
	private void createNewFile(String theFile)	
	{
		mapReader = new FileIO(theFile, FileIO.WRITE);
		try
		{
			String fileData = inputReader.readLine();
			int fileDepth = Integer.parseInt(fileData.substring(fileData.indexOf(" ") + 1));
			String theFileText = "";
			for(int index = 0; index < fileDepth; index++)
			{
				theFileText += inputReader.readLine() + "\n";
			}
			Debug.on();
			Debug.println(theFileText);
			mapReader.writeData(fileData + "\n" + theFileText);
		}
		catch(IOException e) { Debug.println("Error: IO"); }
	}
}
